package com.ctrip.openapi.java.utils;

import cn.buk.util.VerifyCodeUtil;

import java.security.NoSuchAlgorithmException;


public class SignatureUtils {
	// 获得时间戳
	public static long GetTimeStamp() {
		return System.currentTimeMillis()/1000;
	}

	// 获得签名
	public static String CalculationSignature(String timeStamp,
			String allianceID, String secretKey, String sid, String requestType)
			throws Exception {
		StringBuilder stringBuilder = new StringBuilder();

		stringBuilder.append(timeStamp);

		if (Integer.parseInt(allianceID) > 0) {
			stringBuilder.append(allianceID);
		}

		if (secretKey != null && !secretKey.equals("")) {
			String sectetKeyMD5 = VerifyCodeUtil.MD5(secretKey).toUpperCase();
			stringBuilder.append(sectetKeyMD5);
		} else {
			throw new Exception("SecretKey should not be null or empty!");
		}

		String temp1 = "abcdefg123456";
		String temp2 = VerifyCodeUtil.MD5(temp1).toUpperCase();

		if (Integer.parseInt(sid) > 0) {
			stringBuilder.append(sid);
		}

		if (requestType != null && !requestType.trim().equals("")) {
			stringBuilder.append(requestType);
		}

		String result = stringBuilder.toString();

		return VerifyCodeUtil.MD5(result).toUpperCase();
	}

	public static String MD5(String txt) {
		return VerifyCodeUtil.MD5(txt);
	}

}